; Inno Setup Script for DBTools
; Build steps (recommend self-contained publish first):
; dotnet publish .\DBTools\DBTools.csproj -c Release -r win-x64 --self-contained true /p:PublishSingleFile=true /p:PublishTrimmed=false
; Publish output: DBTools\bin\Release\net9.0-windows\win-x64\publish

#define MyAppVersion GetEnv('AppVersion')
#ifndef MyAppVersion
  #define MyAppVersion "1.0.0"
#endif

#define AppId "{{8E72B7E8-1A7F-4F1D-9F2E-1B9B2D7D5F22}}"
#define MyAppPublisher "DBTools"
#define MyAppOutputDir ".\installers"
#define MyAppName "DBTools"
#define MyAppNameCN "DBTools"
#define MyAppNameUninstall "Uninstall DBTools"
#define MyAppExeName "DBTools.exe"
#define MyAppURL "https://example.com/"
; Setup/Uninstall icons (place .ico files under icons directory)
#define MyAppSetupIcon ".\icons\install.ico"
#define MyAppUninstallIcon ".\icons\uninstall.ico"

; Source directories: prefer self-contained publish, fallback to framework-dependent publish
#define PublishDirSelfContained "..\DBTools\bin\Release\net9.0-windows\win-x64\publish"
#define PublishDirFramework "..\DBTools\bin\Release\net9.0-windows\publish"

[Setup]
AppId={#AppId}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppPublisher}\{#MyAppName}
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
OutputDir={#MyAppOutputDir}
OutputBaseFilename={#MyAppName}-{#MyAppVersion}-Setup
SetupIconFile={#MyAppSetupIcon}
UninstallDisplayIcon={#MyAppUninstallIcon}
Compression=lzma
SolidCompression=yes
DirExistsWarning=no
DisableDirPage=no
UsePreviousAppDir=yes
CloseApplications=no
ArchitecturesInstallIn64BitMode=x64

[Languages]
; Use Simplified Chinese UI language for installer
Name: "ChineseSimplified"; MessagesFile: "compiler:Languages\ChineseSimplified.isl"

[Run]
; Kill running instance before install to avoid file locks
Filename: "cmd.exe"; Parameters: "/C taskkill /IM {#MyAppExeName} /F"; Flags: runhidden
; Optionally launch after install
;Filename: "{app}\\{#MyAppExeName}"; Description: "Run {#MyAppNameCN}"; Flags: nowait postinstall skipifsilent

[Tasks]
; Optional: create desktop shortcut
Name: "desktopicon"; Description: "Create Desktop Shortcut"; GroupDescription: "Additional Icons"; Flags: checkablealone

[Files]
; Prefer self-contained publish directory; if missing, use framework-dependent (uncomment below)
Source: "{#PublishDirSelfContained}\\*.*"; DestDir: "{app}"; Excludes: "\\runtimes\\linux*,\\runtimes\\osx*,\\runtimes\\browser*,*.pdb,*.xml"; Flags: ignoreversion recursesubdirs createallsubdirs; BeforeInstall: TaskKill('{#MyAppExeName}')
;Source: "{#PublishDirFramework}\\*.*"; DestDir: "{app}"; Excludes: "\\runtimes\\linux*,\\runtimes\\osx*,\\runtimes\\browser*,*.pdb,*.xml"; Flags: ignoreversion recursesubdirs createallsubdirs; BeforeInstall: TaskKill('{#MyAppExeName}')

[Dirs]
Name: "{app}";

[Icons]
; Desktop shortcut
Name: "{commondesktop}\\{#MyAppNameCN}"; Filename: "{app}\\{#MyAppExeName}"; Tasks: desktopicon
; Start menu
Name: "{group}\\{#MyAppNameCN}"; Filename: "{app}\\{#MyAppExeName}"; WorkingDir: "{app}"
; Uninstall shortcut
Name: "{group}\\{#MyAppNameUninstall}"; Filename: "{uninstallexe}"

[UninstallRun]
; Kill running instance before uninstall
Filename: "cmd.exe"; Parameters: "/C taskkill /IM {#MyAppExeName} /F"; Flags: runhidden

[Code]
#ifndef AppVersion
  #define AppVersion "1.0.0.0"
#endif

procedure TaskKill(FileName: String);
var
  ResultCode: Integer;
begin
  Exec(ExpandConstant('{sys}\\taskkill.exe'), '/f /im ' + '"' + FileName + '"', ExpandConstant('{sys}'), SW_HIDE, ewWaitUntilTerminated, ResultCode);
end;
